package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.MmochatActivityRecord;

public class MmochatActivityRecordDao extends SimpleJdbcDaoSupport {
	private static final ActivityRowMapper activityRowMapper = new ActivityRowMapper();

	// 增加新的活动记录
	private static final String addNewActivityRecordSql = "insert into activity_record(skyid,"
			+ "roleid,count,activity_type,text) values(?,?,?,?,?)";

	public void addNewActivityRecord(long skyId, int roleId, int count,
			int type, String text) {
		getSimpleJdbcTemplate().update(addNewActivityRecordSql, skyId, roleId,
				count, type, text);
	}

	// 查询活动记录
	private static final String getActivityRecordSql = "select * from activity_record where activity_type = ?";

	public List<MmochatActivityRecord> getActivityRecord(int type) {
		return getSimpleJdbcTemplate().query(getActivityRecordSql,
				activityRowMapper, type);
	}

	// 查询活动记录
	private static final String getActivityRecord2Sql = "select * from activity_record"
			+ " where activity_type = ? and time >= ? and time < ? order by time desc";

	public List<MmochatActivityRecord> getActivityRecord2(int type,
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(getActivityRecord2Sql,
				activityRowMapper, type, startDate, endDate);
	}

	static class ActivityRowMapper implements
			ParameterizedRowMapper<MmochatActivityRecord> {
		@Override
		public MmochatActivityRecord mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatActivityRecord record = new MmochatActivityRecord();
			record.setSkyId(rs.getLong("skyid"));
			record.setRoleId(rs.getInt("roleid"));
			record.setCount(rs.getInt("count"));
			record.setType(rs.getInt("activity_type"));
			record.setText(rs.getString("text"));

			return record;
		}
	}
}
